package xiaomin.demo.algorithm;

import java.util.List;

public class Lesson28 {
    static class ListNode{
        int val;
        ListNode next;
        ListNode(int i){
            val=i;
            next=null;
        }
    }

    public static void run(){
        ListNode n1=new ListNode(1);
        n1.next=new ListNode(2);
        n1.next.next=new ListNode(3);
        n1.next.next.next=new ListNode(4);
        ListNode nodeHead=new ListNode(0);
        nodeHead.next=n1;

        reverse(n1,nodeHead);

        ListNode tmp=nodeHead.next;
        while (tmp!=null){
            System.out.println(tmp.val);
            tmp=tmp.next;
        }
    }

    public static ListNode reverse(ListNode nodeHead,ListNode firstNode){
        if(nodeHead.next==null){
            firstNode.next=nodeHead;
            return nodeHead;
        }
        else {
            ListNode lastNode=reverse(nodeHead.next,firstNode);//反转后面一个节点，再返回该节点
            lastNode.next=nodeHead;//后面的节点指向自己
            nodeHead.next=null;//把自己的指针赋值为空
            return nodeHead;//返回当前节点
        }
    }
}
